window.onload=function(){
    var config={
        elem:"#goodsList",
        url:"/shop/goods-index",
        cols:[[
            {type:'numbers', width:80, title: '编号'},
            {field:'goods_name', width:390, title: '商品名称'},
            {field:'org_pre', width:150, title: '价格'},
            {field:'sales_volume', width:100, title: '总销量'},
            {field:'status', width:100, title: '商品状态',templet: function (item) {
                    switch (parseInt(item.status)){
                        case 0:
                            return '下架';
                        case 1:
                            return '上架';
                    }
                }},
            {field:'', width:120, title: 'SKU',toolbar:'#getSKU'},
            {field:'right', minWidth:150, title: '操作',align:'center',toolbar:'#operation2'}
        ]],
        page:true
    }
    layui.use(['element','table','jquery','form','layer'],function(){
        var table=layui.table,$=layui.jquery,form=layui.form;
        // 商品列表
        var id=$("#id").val();
        config.url="/shop/goods-index?id="+id;
        table.render(config)
        table.on('tool(goodsList)',function(obj){
            var data = obj.data;
            var layEvent = obj.event;
            if(layEvent === 'detail'){
                location.href = '/shop/googs-info?id='+data.id;
            }else if(layEvent === 'getsku'){
                var skuTable='';
                $.ajax({
                    url:"/goods/get-goodsattr",
                    type:"get",
                    data:{id:data.id},
                    dataType:"json",
                    async:false,
                    success:function(res){
                        skuTable = '<table class="layui-table" style="max-height:375px;">'
                        skuTable +=     '<colgroup>';

                        $(res.data.thead).each(function(i,item){
                            skuTable += '<col width="120"></col>'
                        })
                                
                        skuTable +=         '<col width="120">'
                        skuTable +=         '<col width="120">'
                        skuTable +=         '<col width="120">'
                        skuTable +=         '<col width="120">'
                        skuTable +=         '<col width="120">'
                        skuTable +=     '</colgroup>'
                        skuTable +=     '<thead><tr>'
                        $(res.data.thead).each(function(i,item){
                            skuTable += '<th>'+item+'</th>'
                        })
                        skuTable +=         '<th>原价</th><th>折扣价</th><th>拼团库存</th>'
                        skuTable +=     '</tr></thead>'
                        skuTable +=     '<tbody>'
                        $(res.data.tbody).each(function(i,item){
                            skuTable += '<tr>'
                            $(item.val).each(function(i,item){
                                skuTable += '<th>'+item.value+'</th>'
                            })
                            skuTable +=         '<td>'+item.original_price+'</td>'
                            skuTable +=         '<td>'+item.discount_price+'</td>'
                            skuTable +=         '<td>'+item.inventory+'</td>'
                            skuTable += '</tr>'
                        })
                        skuTable +=     '</tbody>'
                        skuTable += '</table>'
                    }
                })
                Utils.layer.open({
                    type:1,
                    title:"SKU",
                    area:['800px','auto'],
                    content:skuTable
                })
            }
        })
        form.on('select(one)',function(data){
            var id=data.value;
            $('.twoclass').html('');
            $('.threeclass').html('<option value="0">请选择三级分类</option>');
            if(id!=0){
                $.ajax({
                    'url': '/goods/get-tree',
                    'type': 'POST',
                    'dataType': 'json',
                    'data': {id:id},
                    'success': function (data) {
                        if (data.code != 200) {
                            layer.alert(data.msg);
                            return false;
                        } else {
                            var option = '<option value="0">请选择二级分类</option>';
                            $.each(data.data,function(indexe,value){
                                option += '<option value="'+value.id+'">'+value.tree_name+'</option>'
                            });
                            $('.twoclass').append(option);
                            form.render('select')
                        }
                    }
                });
            }else{
                form.render('select')
            }
        })
        form.on('select(two)',function(data){
            var id=data.value;
            $('.threeclass').html('');
            if(id!=0){
                $.ajax({
                    'url': '/goods/get-tree',
                    'type': 'POST',
                    'dataType': 'json',
                    'data': {id:id},
                    'success': function (data) {
                        if (data.code != 200) {
                            layer.alert(data.msg);
                            return false;
                        } else {
                            var option = '<option value="0">请选择三级分类</option>';
                            $.each(data.data,function(indexe,value){
                                option += '<option value="'+value.id+'">'+value.tree_name+'</option>'
                            });
                            $('.threeclass').append(option);
                            form.render('select')
                        }
                    }
                });
            }else{
                form.render('select');
            }
        })
        form.on("submit(serch-form)",function(data){
            var query = $("#form1").serialize();
            var url = '/shop/goods-index?' + query;
            config.url = url;
            table.render(config);
            return false;

        });

        var hf=window.location.href.split("?");
        var hf2=hf[1].split("&");
        var hf3=hf2[1];
        var id=hf2[0].split("=")[1];
        if(hf3!=undefined){
            $(".a1").attr("href","/shop/get-info?id="+id+"&type=1");
            $(".a2").attr("href","/shop/goods-index?id="+id+"&type=1");
            $(".a3").attr("href","/shop/activity?id="+id+"&type=1");
        }else{
            $(".a1").attr("href","/shop/get-info?id="+id);
            $(".a2").attr("href","/shop/goods-index?id="+id);
            $(".a3").attr("href","/shop/activity?id="+id);
        }
        
        $('.back').on("click",function(){
            if(hf3!=undefined){
                this.href="/shop-user/index";
            }else{
                this.href="/shop/index";
            }
        })
    })
};